﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BitPortal.Models;
using BitPortal.Utils;
using BitPortal.Business;
using System.Web.Security;
namespace BitPortal.Admin
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SiteMapNode siteNode = SiteMap.RootNode;
            Ext.Net.TreeNode root = this.CreateNode(siteNode);
            TreePanel1.Root.Add(root);
            TreePanel1.RootVisible = false;
        }
        //static node creation with children
        private Ext.Net.TreeNode CreateNode(SiteMapNode siteMapNode)
        {
            Ext.Net.TreeNode treeNode = new Ext.Net.TreeNode();

            if (!string.IsNullOrEmpty(siteMapNode.Url))
            {
                treeNode.Href = this.Page.ResolveUrl(siteMapNode.Url);
            }

            treeNode.NodeID = siteMapNode.Key;
            treeNode.Text = siteMapNode.Title;
            treeNode.Qtip = siteMapNode.Description;

            SiteMapNodeCollection children = siteMapNode.ChildNodes;

            if (children != null && children.Count > 0)
            {
                foreach (SiteMapNode mapNode in siteMapNode.ChildNodes)
                {
                    bool flag = false;
                    if (mapNode.Roles.Count > 0)
                    {
                        foreach (string role in mapNode.Roles)
                        {
                            if (Roles.IsUserInRole(role)) flag = true;
                        }
                    }
                    else
                    {
                        flag = true;
                    }
                    if (flag) treeNode.Nodes.Add(this.CreateNode(mapNode));
                }
            }

            return treeNode;
        }

        protected override void OnInit(EventArgs e)
        {
            if (BitPortal.Business.BitContext.Current.LoginUser == null)
            {
                ExceptionHander.ThrowAdminLoginRequireException();
            }
            if (!(Roles.IsUserInRole("Admin") || Roles.IsUserInRole("RootAdmin")))
            {
                ExceptionHander.ThrowAdminLoginRequireException();
            }
            base.OnInit(e);
        }
    }
}